/**
 * kenny 
 * 2013-2-5
 */
package com.handinfo.android.ui.window;

import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;

import android.R;
import android.R.integer;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.graphics.Bitmap;

import com.handinfo.android.DWGameActivity;
import com.handinfo.android.DWGameManager;
import com.handinfo.android.core.graphics.DWBitmap;
import com.handinfo.android.core.graphics.DWFont;
import com.handinfo.android.core.net.GameProtocol;
import com.handinfo.android.core.resource.DWSerializableFactory;
import com.handinfo.android.core.resource.ResHead;
import com.handinfo.android.core.resource.ResourceManager;
import com.handinfo.android.game.Achievement;
import com.handinfo.android.game.IconManger;
import com.handinfo.android.ui.IUIWindows;
import com.handinfo.android.ui.UIWindows;
import com.handinfo.android.uicontrols.DWControlTools;
import com.handinfo.android.uicontrols.DWControlsManager;
import com.handinfo.android.uicontrols.DWListener;
import com.handinfo.android.uicontrols.controls.DWBackground;
import com.handinfo.android.uicontrols.controls.DWButton;
import com.handinfo.android.uicontrols.controls.DWControl;
import com.handinfo.android.uicontrols.controls.DWFrame;
import com.handinfo.android.uicontrols.controls.DWImageBox;
import com.handinfo.android.uicontrols.controls.DWListSubItem;
import com.handinfo.android.uicontrols.controls.DWListbox;
import com.handinfo.android.uicontrols.controls.DWTextbox;
import com.handinfo.android.uicontrols.controls.DWTitle;
import com.handinfo.android.utils.Tools;

public class UIAchievement implements IUIWindows{

	private boolean m_isOpen = false;
	private DWFrame m_achi_frame = null;//frame
	private DWTitle m_achi_titile = null;//标题
	private DWImageBox m_image_title = null;//fame图片字
	private Bitmap m_bm_title = null;
	private DWImageBox m_achi_close = null;//关闭按钮
	private Bitmap m_bm_close = null;
	private DWBackground m_achi_backGround = null;//大的背景
	private Bitmap m_bm_backGround = null;
	private DWListbox m_left_listBox = null;//左侧按钮列表
	private DWButton[] m_achi_button;//左侧按钮
	private Bitmap m_bm_button01 = null;//明
	private Bitmap m_bm_button02 = null;//暗
	private DWListSubItem[] m_left_subItem;//左侧subItem个数
	private DWListbox m_right_listBox = null;
	//右侧 listBox
	private DWListSubItem[] m_right_subItem;
	private DWImageBox[] m_chengjiu_beijing;//成就的背景图
	private Bitmap m_bm_beijing = null;
	private DWImageBox[] m_chengjiu_icon;//成就图标
	private Bitmap[] m_bm_icon;
	private DWTextbox[] m_chengjiu_title;//成就名字
	private DWTextbox[] m_chengjiu_jiangli;//成就奖励
	private DWTextbox[] m_chengjiu_yaoqiu;//成就奖励
	private DWImageBox[] m_chengjiu_zhuangtai;//成就状态
	private Bitmap m_chengjiu_ming = null;
	private Bitmap m_chengjiu_an = null;
	private Bitmap m_bm_yilingqu = null;//已经领取
	private Bitmap m_bm_weiwancheng = null;//未完成
	private DWButton[] m_chengjiu_lingqu;//领取按钮
	private int m_item_size = 0;//item个数
	private int[] m_item_stauts;//成就状态
	//数据
	private String[] m_button_str = {"1","2","3","4"};
	private int m_button_size = m_button_str.length;//按钮个数
	private int m_index = 0;//选择页码分组
	
	private Bitmap m_chengjiu;
	private Bitmap m_chengjiuname;
	private DWImageBox[] m_chengjiu_ds;
	private DWImageBox[] m_chengjiu_bk;
	private DWTextbox[] m_chengjiu_dstb;
	
	private DWImageBox m_chengjiu_dsx;
	private DWTextbox m_chengjiu_dstbx;
	
	private int m_plate;//总奖杯点数
	
	private Achievement m_ach[];

	
	private void openAchi(){
		m_achi_frame = new DWFrame(DWFrame.FRAME_TYPE_FULL, true);	
		m_achi_frame.setClickClose(false);
		m_achi_titile = new DWTitle("", m_achi_frame);
		m_image_title = new DWImageBox(m_bm_title);//图片字图标
		m_image_title.setNearAnchor(m_achi_titile, Tools.HCENTER_VCENTER, Tools.HCENTER_VCENTER, 0, 0);
		//关闭按钮
		m_achi_close = new DWImageBox(m_bm_close);
		m_achi_close.addListener(m_close);
		m_achi_close.setNearAnchor(m_achi_titile, Tools.RIGHT_BOTTOM, Tools.RIGHT_BOTTOM, 0, 0);
		//大背景
		m_achi_backGround = new DWBackground(m_achi_frame.getShowWidth()-m_achi_close.getShowWidth(), m_achi_frame.getShowHeight()-m_achi_close.getShowHeight());
		m_achi_backGround.setFill(true);//设置透明效果
		m_achi_backGround.setNearAnchor(m_achi_frame, Tools.HCENTER_VCENTER, Tools.HCENTER_VCENTER, 0, 0);
		//左侧listbox
		m_left_listBox = new DWListbox(m_achi_frame.getShowWidth()*25/128-10, m_achi_frame.getShowHeight() - m_achi_frame.getShowHeight()*20/72);	
		m_left_listBox.setShowBackgroundRect(false);
		m_left_listBox.setLineSpacing(m_achi_frame.getShowWidth()*3/128);
		m_left_listBox.setNearAnchor(m_achi_backGround, Tools.LEFT_VCENTER, Tools.LEFT_VCENTER, DWGameManager.Screen_Width/128, 0);
		
		
		//刷新 按钮个数
		refreshButtonNum();
		
		//右侧listBox
		m_right_listBox = new DWListbox(m_achi_backGround.getShowWidth()*100/128, m_achi_backGround.getShowHeight() - m_achi_backGround.getShowHeight()*5/72);
		m_right_listBox.setShowBackgroundRect(false);
		m_right_listBox.setLineSpacing(m_achi_frame.getShowWidth()/128);
		m_right_listBox.setNearAnchor(m_left_listBox, Tools.LEFT_VCENTER, Tools.RIGHT_VCENTER, DWGameManager.Screen_Width/128+10, 0);
		
		//成就总数
		m_chengjiu_dsx = new DWImageBox(m_chengjiu);
		m_chengjiu_dsx.setNearAnchor(m_left_listBox, Tools.HCENTER_TOP, Tools.HCENTER_BOTTOM, -20,10);
		m_chengjiu_dstbx = new DWTextbox(""+m_plate);
		m_chengjiu_dstbx.setNearAnchor(m_chengjiu_dsx, Tools.LEFT_TOP, Tools.LEFT_TOP, 5+m_chengjiu_dsx.getShowWidth(), 0);
		
		//刷新右侧成就
		refreshRight();
		
		if(m_open_index == 1){
			m_index = m_class;
			refreshRight();
			m_right_listBox.setScrollbar(m_id/2);
			for(int i=0;i<m_achi_button.length;i++){
				if(i == m_index){
					m_achi_button[i].setTextColor(0xFFFFFFFF);
					//m_achi_button[i].setBackground(m_bm_button01, false);
				}else{
					m_achi_button[i].setTextColor(0xFF898989);
//					m_achi_button[i].setBackground(m_bm_button02, false);
				}
			}
		
		}
		
		m_achi_frame.addControl(m_achi_titile);
		m_achi_frame.addControl(m_image_title);
		m_achi_frame.addControl(m_achi_close);
		m_achi_frame.addControl(m_achi_backGround);
		m_achi_frame.addControl(m_left_listBox);
		m_achi_frame.addControl(m_right_listBox);
		m_achi_frame.addControl(m_chengjiu_dsx);
		m_achi_frame.addControl(m_chengjiu_dstbx);
		DWControlsManager.getInstance().addControl(m_achi_frame);
	}
	
	//刷新 按钮个数
	private void refreshButtonNum(){
		if(m_left_listBox != null){
			m_left_listBox.removeAllSubItem();
		}
		m_achi_button = new DWButton[m_button_size];
		m_left_subItem = new DWListSubItem[m_button_size];
		for(int i=0;i<m_button_size;i++){
			final int index = i;
			m_left_subItem[i] = new DWListSubItem();
			m_achi_button[i] = new DWButton(m_button_str[i], m_bm_button01);
			m_achi_button[i].setTextColor(0xFF898989);
			if(i == m_index){
				m_achi_button[i].setTextColor(0xFFFFFFFF);
				//m_achi_button[i].setBackground(m_bm_button01, false);
			}else{
				m_achi_button[i].setTextColor(0xFF898989);
//				m_achi_button[i].setBackground(m_bm_button02, false);
			}
			m_achi_button[i].setDownImage(m_bm_button02);
			m_achi_button[i].setNearAnchor(m_left_subItem[i], Tools.LEFT_TOP, Tools.LEFT_TOP, 0, 0);
			m_left_subItem[i].addControls(m_achi_button[i]);
			m_left_listBox.addSubItem(m_left_subItem[i]);
			//按钮点击
			m_achi_button[index].addListener(new DWListener() {
				
				public void OnDoubleClick() {
				}
				
				public void OnClick() {
					m_index = index;
					for(int i=0;i<m_achi_button.length;i++){
						if(i == m_index){
							m_achi_button[i].setTextColor(0xFFFFFFFF);
							//m_achi_button[i].setBackground(m_bm_button01, false);
						}else{
							m_achi_button[i].setTextColor(0xFF898989);
//							m_achi_button[i].setBackground(m_bm_button02, false);
						}
					}
					refreshRight();
					
				}
			});
		}
	}
	
	//刷新右侧成就
	private void refreshRight(){
		if(m_arrayList==null){
			DWGameManager.getInstance().addSystemInfo(
					DWGameManager.SYSTEM_INFO_TYPE_B, "m_arrayList错误！");
			return;
		}
		if(m_arrayList[m_index]==null){
			DWGameManager.getInstance().addSystemInfo(
					DWGameManager.SYSTEM_INFO_TYPE_B, "m_arrayList"+m_index+"错误！");
			return;
		}
		
		
		if(m_right_listBox != null){
			m_right_listBox.removeAllSubItem();
		}
		m_item_size = m_arrayList[m_index].size();
		m_right_subItem = new DWListSubItem[m_item_size];
		m_chengjiu_beijing = new DWImageBox[m_item_size];//成就背景
		m_chengjiu_icon = new DWImageBox[m_item_size];//成就图标
		m_bm_icon = new Bitmap[m_item_size];
		m_chengjiu_title = new DWTextbox[m_item_size];//成就名字
		m_chengjiu_jiangli = new DWTextbox[m_item_size];//成就奖励
		m_chengjiu_yaoqiu = new DWTextbox[m_item_size];//成就奖励
		m_chengjiu_zhuangtai = new DWImageBox[m_item_size];//成就状态
		m_chengjiu_lingqu = new DWButton[m_item_size];//领取按钮
		m_item_stauts = new int[m_item_size];//成就状态
		
		m_chengjiu_ds = new DWImageBox[m_item_size];//成就点数
		m_chengjiu_bk = new DWImageBox[m_item_size];//成就名字背景
		m_chengjiu_dstb = new DWTextbox[m_item_size];//成就点数
		
		for(int i = 0; i < m_item_size/2+m_item_size%2;i++){
			m_right_subItem[i] = new DWListSubItem();
		}
		//循环画出item
		for(int i=0;i<m_item_size;i++){
			final int index = i;
			//小背景	
			m_chengjiu_beijing[i] = new DWImageBox(m_bm_beijing);
			m_chengjiu_beijing[i].setShowWideHigh(m_right_listBox.getShowWidth()/2 - 10, m_right_listBox.getShowHeight()/3-10);			
			if(i%2 == 0){//左侧
				//小背景	
				m_chengjiu_beijing[i].setNearAnchor(m_right_subItem[i/2], Tools.LEFT_TOP, Tools.LEFT_TOP, 0, 0);
				
				refreshIcon(i);
				m_right_subItem[i/2].addControls(m_chengjiu_beijing[i]);
				m_right_subItem[i/2].addControls(m_chengjiu_icon[i]);
				m_right_subItem[i/2].addControls(m_chengjiu_bk[i]);
				m_right_subItem[i/2].addControls(m_chengjiu_title[i]);
				m_right_subItem[i/2].addControls(m_chengjiu_jiangli[i]);
				m_right_subItem[i/2].addControls(m_chengjiu_yaoqiu[i]);
				m_right_subItem[i/2].addControls(m_chengjiu_ds[i]);
				m_right_subItem[i/2].addControls(m_chengjiu_dstb[i]);
		
				if(m_arrayList[m_index].get(i).getState() == Achievement.已完成未领奖){
					m_right_subItem[i/2].addControls(m_chengjiu_lingqu[i]);
				}
				m_right_subItem[i/2].addControls(m_chengjiu_zhuangtai[i]);
				m_right_listBox.addSubItem(m_right_subItem[i/2]);
				
			}else{//右侧
				m_chengjiu_beijing[i].setNearAnchor(m_right_subItem[i-i/2-i%2], Tools.LEFT_TOP, Tools.LEFT_TOP, m_chengjiu_beijing[i].getShowWidth()+10, 0);
				
				refreshIcon(i);
				m_right_subItem[i-i/2-i%2].addControls(m_chengjiu_beijing[i]);
				m_right_subItem[i-i/2-i%2].addControls(m_chengjiu_icon[i]);
				m_right_subItem[i-i/2-i%2].addControls(m_chengjiu_bk[i]);
				m_right_subItem[i-i/2-i%2].addControls(m_chengjiu_title[i]);
				m_right_subItem[i-i/2-i%2].addControls(m_chengjiu_jiangli[i]);
				m_right_subItem[i-i/2-i%2].addControls(m_chengjiu_yaoqiu[i]);
				m_right_subItem[i-i/2-i%2].addControls(m_chengjiu_ds[i]);
				m_right_subItem[i-i/2-i%2].addControls(m_chengjiu_dstb[i]);
				if(m_arrayList[m_index].get(i).getState() == Achievement.已完成未领奖){
					m_right_subItem[i-i/2-i%2].addControls(m_chengjiu_lingqu[i]);
				}
				m_right_subItem[i-i/2-i%2].addControls(m_chengjiu_zhuangtai[i]);
				
			}
			//成就领取按钮
			m_chengjiu_lingqu[index].addListener(new DWListener() {
				
				public void OnDoubleClick() {
					
				}
				
				public void OnClick() {
//					DWGameManager.getInstance().addSystemInfo(
//							DWGameManager.SYSTEM_INFO_TYPE_B, "点击-1--！"+index);
					sendLingqu(m_arrayList[m_index].get(index).getId());
					for(int i=0;i<m_ach.length;i++){
						if(m_ach[i].getId() == m_lingquid){
							m_ach[i].setState(m_ach[i].完成已领奖);
							getArrayListGroup();
							refreshRight();
							return;
						}
					}

//					DWGameManager.getInstance().addSystemInfo(
//							DWGameManager.SYSTEM_INFO_TYPE_B, "点击-2--！"+index);
				}
			});

		}
		
	}
/*	
	//绘制的封装
	private void refreshIcon1(int i){
		//成就图标
		m_bm_icon[i] = IconManger.getInstance().getPinzhi(m_arrayList[m_index].get(i).getIcon());
		m_chengjiu_icon[i] = new DWImageBox(m_bm_icon[i]);
		m_chengjiu_icon[i].setNearAnchor(m_chengjiu_beijing[i], Tools.LEFT_VCENTER, Tools.LEFT_VCENTER, DWGameManager.Screen_Width/128, 0);
		//成就标题
		m_chengjiu_title[i] = new DWTextbox("<mark c=FFBB11>"+m_arrayList[m_index].get(i).getTitle()+":</mark>");
		m_chengjiu_title[i].setNearAnchor(m_chengjiu_beijing[i], Tools.HCENTER_TOP, Tools.HCENTER_TOP,0,DWGameManager.Screen_Height/72);
		//成就奖励
		m_chengjiu_jiangli[i] = new DWTextbox(m_arrayList[m_index].get(i).getCondition());
		m_chengjiu_jiangli[i].setNearAnchor(m_chengjiu_icon[i], Tools.LEFT_VCENTER, Tools.RIGHT_VCENTER,DWGameManager.Screen_Height/72,0);
		//成就状态
		//按钮
		m_chengjiu_lingqu[i] = new DWButton("领取", m_chengjiu_ming);
		m_chengjiu_lingqu[i].setDownImage(m_chengjiu_an);
		m_chengjiu_lingqu[i].setNearAnchor(m_chengjiu_beijing[i], Tools.LEFT_BOTTOM, Tools.HCENTER_BOTTOM, 0, 0);
		//图片
		m_chengjiu_zhuangtai[i] = new DWImageBox(m_bm_yilingqu);
		m_chengjiu_zhuangtai[i].setShow(false);
		m_chengjiu_zhuangtai[i].setTouch(false);
		m_chengjiu_zhuangtai[i].setNearAnchor(m_chengjiu_beijing[i], Tools.LEFT_BOTTOM, Tools.HCENTER_BOTTOM, 0, 0);
		if(m_arrayList[m_index].get(i).getState() == Achievement.未完成){//未完成
			m_chengjiu_lingqu[i].setShow(false);
			m_chengjiu_lingqu[i].setTouch(false);
			m_chengjiu_zhuangtai[i].setShow(true);
			m_chengjiu_zhuangtai[i].setBackground(m_bm_weiwancheng, false);
		}else if(m_arrayList[m_index].get(i).getState() == Achievement.已完成未领奖){//已完成未领奖 = 1;
			m_chengjiu_lingqu[i].setShow(true);
			m_chengjiu_lingqu[i].setTouch(true);
			m_chengjiu_zhuangtai[i].setShow(false);
			
		}else if(m_arrayList[m_index].get(i).getState() == Achievement.完成已领奖){//完成已领奖 = 2;
			m_chengjiu_lingqu[i].setShow(false);
			m_chengjiu_lingqu[i].setTouch(false);
			m_chengjiu_zhuangtai[i].setShow(true);
			m_chengjiu_zhuangtai[i].setBackground(m_bm_yilingqu, false);
			
		}
	}
	*/
	//绘制的封装
		private void refreshIcon(int i){
			
			//成就标题
			m_chengjiu_title[i] = new DWTextbox("<mark c=FFBB11>"+m_arrayList[m_index].get(i).getTitle().substring(0, 2)+"</mark>"+"`<mark c=FFBB11>"+m_arrayList[m_index].get(i).getTitle().substring(2)+"</mark>");
			m_chengjiu_title[i].setNearAnchor(m_chengjiu_beijing[i], Tools.LEFT_VCENTER, Tools.LEFT_VCENTER, DWGameManager.Screen_Width/128+DWGameManager.Screen_Width*7/800, 0);
			
			//成就标题背景
			m_chengjiu_bk[i] = new DWImageBox(m_chengjiuname);
			m_chengjiu_bk[i].setNearAnchor(m_chengjiu_title[i], Tools.HCENTER_VCENTER, Tools.HCENTER_VCENTER, 0, 0);
			
			//成就奖励
			m_chengjiu_jiangli[i] = new DWTextbox(m_arrayList[m_index].get(i).getPrize());
			m_chengjiu_jiangli[i].setNearAnchor(m_chengjiu_title[i], Tools.LEFT_VCENTER, Tools.RIGHT_VCENTER,DWGameManager.Screen_Height/72+14,0);
			//成就要求
			m_chengjiu_yaoqiu[i] = new DWTextbox(m_arrayList[m_index].get(i).getCondition());
//			m_chengjiu_yaoqiu[i].setNearAnchor(m_chengjiu_beijing[i], Tools.HCENTER_TOP, Tools.HCENTER_TOP,0,DWGameManager.Screen_Height/72);
			m_chengjiu_yaoqiu[i].setNearAnchor(m_chengjiu_title[i], Tools.LEFT_VCENTER, Tools.RIGHT_VCENTER,DWGameManager.Screen_Height/72+DWGameManager.Screen_Width*14/800,0-DWFont.getDefaultFont().getHeight()-10);
			
			//成就点数
			m_chengjiu_ds[i] = new DWImageBox(m_chengjiu);
			m_chengjiu_ds[i].setNearAnchor(m_chengjiu_beijing[i], Tools.LEFT_BOTTOM, Tools.HCENTER_BOTTOM, -70, 0);
			m_chengjiu_dstb[i] = new DWTextbox(""+m_arrayList[m_index].get(i).getPlate());
			m_chengjiu_dstb[i].setNearAnchor(m_chengjiu_ds[i], Tools.LEFT_TOP, Tools.LEFT_TOP, 5+m_chengjiu_ds[i].getShowWidth(), 0);
			
			//按钮
			m_chengjiu_lingqu[i] = new DWButton("领取", m_chengjiu_ming);
			m_chengjiu_lingqu[i].setDownImage(m_chengjiu_an);
			m_chengjiu_lingqu[i].setNearAnchor(m_chengjiu_beijing[i], Tools.LEFT_BOTTOM, Tools.HCENTER_BOTTOM, 30, 0);
			//图片
			m_chengjiu_zhuangtai[i] = new DWImageBox(m_bm_yilingqu);
			m_chengjiu_zhuangtai[i].setShow(false);
			m_chengjiu_zhuangtai[i].setTouch(false);
			m_chengjiu_zhuangtai[i].setNearAnchor(m_chengjiu_beijing[i], Tools.LEFT_BOTTOM, Tools.HCENTER_BOTTOM, 30, 0);
			if(m_arrayList[m_index].get(i).getState() == Achievement.未完成){//未完成
				m_chengjiu_lingqu[i].setShow(false);
				m_chengjiu_lingqu[i].setTouch(false);
				m_chengjiu_zhuangtai[i].setShow(true);
				m_chengjiu_zhuangtai[i].setBackground(m_bm_weiwancheng, false);
			}else if(m_arrayList[m_index].get(i).getState() == Achievement.已完成未领奖){//已完成未领奖 = 1;
				m_chengjiu_lingqu[i].setShow(true);
				m_chengjiu_lingqu[i].setTouch(true);
				m_chengjiu_zhuangtai[i].setShow(false);
				
			}else if(m_arrayList[m_index].get(i).getState() == Achievement.完成已领奖){//完成已领奖 = 2;
				m_chengjiu_lingqu[i].setShow(false);
				m_chengjiu_lingqu[i].setTouch(false);
				m_chengjiu_zhuangtai[i].setShow(true);
				m_chengjiu_zhuangtai[i].setBackground(m_bm_yilingqu, false);
			}
		}
	
	public void init() {
		m_bm_close = UIWindows.createImage("/img/newui/fanhuianniu_1.gnp");//返回按钮图标
		m_bm_title = UIWindows.createImage("/img/newui/chengjiujiangli_1.gnp");//标题图标
		m_bm_button01 = UIWindows.createImage("/img/newui/anniu_6.gnp");//左侧明按钮
		m_bm_button02 = UIWindows.createImage("/img/newui/anniu_6ax.gnp");//左侧暗按钮
		//成就相关图片
		m_chengjiu_ming = UIWindows.createImage("/img/newui/anniu_14.gnp");//成就栏明按钮
		m_chengjiu_an = UIWindows.createImage("/img/newui/anniu_14ax.gnp");//成就栏暗按钮		
		m_bm_beijing = UIWindows.createImage("/img/newui/chongzhidi_1.gnp");//成就背景
		m_bm_yilingqu = UIWindows.createImage("/img/newui/yilingqu_1.gnp");//已经领取
		m_bm_weiwancheng = UIWindows.createImage("/img/newui/weiwancheng_1.gnp");//未完成
		
		m_chengjiu = UIWindows.createImage("/img/newui/chengjiu_3.gnp");//未完成
		m_chengjiuname = UIWindows.createImage("/img/newui/chengjiubj_1.gnp");//未完成
		
		try {
			initData();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		initdrawchengjiu();
	}
	
	//关闭窗口
	private DWListener m_close = new DWListener() {
		
		public void OnDoubleClick() {
		}
		
		public void OnClick() {
			UIWindows.getInstance().m_achie.close((byte)0);
		}
	};
	private byte m_open_index;
	public void open(byte index) {
		if(!m_isOpen){
			m_open_index = index;
//			if(index == 1){
//				for(int i=0;i<m_achi_button.length;i++){
//					if(i == m_index){
//						m_achi_button[i].setTextColor(0xFFFFFFFF);
//						//m_achi_button[i].setBackground(m_bm_button01, false);
//					}else{
//						m_achi_button[i].setTextColor(0xFF898989);
////						m_achi_button[i].setBackground(m_bm_button02, false);
//					}
//				}
//			}
			openAchi();
			m_isOpen = true;
		}else{
			if(m_achi_frame != null){
				DWControlsManager.getInstance().addControl(m_achi_frame);
			}else{
				return;
			}
		}
	
		
	}

	public void close(byte index) {
		m_isOpen = false;
		DWControlsManager.getInstance().removeControl(m_achi_frame);
	}

	public void dispose() {
		// TODO Auto-generated method stub
		
	}

	public void logic() {
		// TODO Auto-generated method stub
//		drawchengjiu();
	}
	
	/**
	 * ArrayList分组
	 */
	public void getArrayListGroup(){
		ArrayList<Achievement> arrayList = new ArrayList<Achievement>();
		m_arrayList = null;
		m_arrayList = new ArrayList[m_button_size];
		for(int i=0;i<m_button_size;i++){
			m_arrayList[i] = new ArrayList<Achievement> ();
		}
		for(int j=0;j<m_arrayList.length;j++){
			for(int i = 0;i<m_ach.length;i++){
				if(m_ach[i].getGroup()==j){
					arrayList.add(m_ach[i]);	
				}
			}
			for(int i=0;i<arrayList.size();i++){
				for(int s=0;s<arrayList.size();s++){
					if(j == arrayList.get(s).getGroup()&&i == arrayList.get(s).getOrder()){
						m_arrayList[j].add(arrayList.get(s));
					}
				}
			}
		}
		

	}
	/**
	 * 查找Achievement
	 * @param id
	 * @return
	 */
	public Achievement seekAchievement(short id){
		for(int i = 0;i<m_ach.length;i++){
			if(id == m_ach[i].getId()){
				return m_ach[i];
			}
		}
		return null;
	}

	public void recvAchieve(DataInputStream dis) throws IOException {
		Tools.debugPrintln("进入同步成就状态"+GameProtocol.SC_ACHIEVEMENT_ALL);
		m_plate = dis.readInt();
		short shuliang = dis.readShort();
		Tools.debugPrintln("进入同步"+m_plate);
		if(shuliang>m_ach.length){
			
			Tools.debugPrintln("服务器："+shuliang+"客户端："+m_ach.length);
			DWGameManager.getInstance().addSystemInfo(
					DWGameManager.SYSTEM_INFO_TYPE_B, "成就列表错误！");
			return;
		}
		for(int i=0;i<shuliang;i++){
			short id = dis.readShort();
			byte state = dis.readByte();
			m_ach[i].setState(state);
			Tools.debugPrintln(m_ach[i].getId()+"状态："+m_ach[i].getState()+"state"+state);
		}
		getArrayListGroup();
//		open((byte)0);
	}
	private short m_lingquid = 0;
	public void recvLingqu(DataInputStream dis) throws IOException {
		Tools.debugPrintln("进入同步领取成就状态"+GameProtocol.SC_ACHIEVEMENT_GET);
		byte state = dis.readByte();
		switch (state) {
		case 0:
			DWGameManager.getInstance().addSystemInfo(
					DWGameManager.SYSTEM_INFO_TYPE_B, "领取失败！");
			for(int i=0;i<m_ach.length;i++){
				if(m_ach[i].getId() == m_lingquid){
					m_ach[i].setState(m_ach[i].已完成未领奖);
					getArrayListGroup();
					refreshRight();
					return;
				}
			}
			break;
		case 1:
			DWGameManager.getInstance().addSystemInfo(
					DWGameManager.SYSTEM_INFO_TYPE_B, "领取成功！");
			for(int i=0;i<m_ach.length;i++){
				if(m_ach[i].getId() == m_lingquid){
					m_ach[i].setState(m_ach[i].完成已领奖);
					m_plate = dis.readInt();
					m_chengjiu_dstbx.setText(""+m_plate);
					getArrayListGroup();
					refreshRight();
					return;
				}
			}
			break;

		default:
			break;
		}
	}
	private int m_class = 0;
	private int m_id = 0;
	public void recvAchieveId(DataInputStream dis) throws IOException {
		Tools.debugPrintln("进入服务器推送成就完成"+GameProtocol.SC_ACHIEVEMENT_STATE);

		short id = dis.readShort();
		byte state = dis.readByte();
		for(int i=0;i<m_ach.length;i++){
			if(m_ach[i].getId() == id){
				m_ach[i].setState(state);
				m_class = m_ach[i].getGroup();
				m_id = m_ach[i].getOrder();
				
				
				getArrayListGroup();
				UIWindows.getInstance().m_main.m_chengjiu_title.setText("<mark c=FFBB11>"+m_ach[i].getTitle().substring(0, 2)+"</mark>"
				+"`<mark c=FFBB11>"+m_ach[i].getTitle().substring(2)+"</mark>");
				
				UIWindows.getInstance().m_main.m_chengjiu_yaoqiu.setText("<mark c=D7C346>"+m_ach[i].getCondition()+"</mark>");
				UIWindows.getInstance().m_main.m_chengjiu_yaoqiu1.setText(m_ach[i].getPrize());
				
				
				
				UIWindows.getInstance().m_main.m_chengjiu_ds.setText(""+m_ach[i].getPlate());
				
				
				int a = UIWindows.getInstance().m_main.m_chengjiu_yaoqiu.getShowWidth();
				int b = UIWindows.getInstance().m_main.m_chengjiu_yaoqiu1.getShowWidth()+UIWindows.getInstance().m_main.m_chengjiu_ds.getShowWidth()+UIWindows.getInstance().m_main.m_main_chengjiu4.getShowWidth();
				
				DWControl constrol = UIWindows.getInstance().m_main.m_main_chengjiu1;
				
				if(a>b){
					UIWindows.getInstance().m_main.m_chengjiu_title.setNearAnchor(constrol, Tools.LEFT_TOP, Tools.LEFT_TOP, 0
							+(UIWindows.getInstance().m_main.m_main_chengjiu1.getShowWidth() - (UIWindows.getInstance().m_main.m_main_chengjiu2.getShowWidth()+a))>>1
							, (constrol.getShowHeight()-UIWindows.getInstance().m_main.m_main_chengjiu2.getShowHeight())>>1);
				}else{
					UIWindows.getInstance().m_main.m_chengjiu_title.setNearAnchor(constrol, Tools.LEFT_TOP, Tools.LEFT_TOP,0
							+(UIWindows.getInstance().m_main.m_main_chengjiu1.getShowWidth() - (UIWindows.getInstance().m_main.m_main_chengjiu2.getShowWidth()+b))>>1
							, (constrol.getShowHeight()-UIWindows.getInstance().m_main.m_main_chengjiu2.getShowHeight())>>1);
				}
				UIWindows.getInstance().m_main.m_main_chengjiu2.setNearAnchor(UIWindows.getInstance().m_main.m_chengjiu_title, Tools.HCENTER_VCENTER, Tools.HCENTER_VCENTER, 0, 0);
				UIWindows.getInstance().m_main.m_chengjiu_yaoqiu.setNearAnchor(UIWindows.getInstance().m_main.m_main_chengjiu2, Tools.LEFT_TOP, Tools.RIGHT_TOP,10,0);
				UIWindows.getInstance().m_main.m_chengjiu_yaoqiu1.setNearAnchor(UIWindows.getInstance().m_main.m_chengjiu_yaoqiu, Tools.LEFT_TOP, Tools.LEFT_BOTTOM,0,0);
				UIWindows.getInstance().m_main.m_main_chengjiu4.setNearAnchor(UIWindows.getInstance().m_main.m_chengjiu_yaoqiu1, Tools.LEFT_BOTTOM, Tools.RIGHT_BOTTOM, 10, 0);
				UIWindows.getInstance().m_main.m_chengjiu_ds.setNearAnchor(UIWindows.getInstance().m_main.m_main_chengjiu4, Tools.LEFT_BOTTOM, Tools.RIGHT_BOTTOM, 5, 0);
				
				
				
				UIWindows.getInstance().m_main.m_main_chengjiu.setShow(true);
				UIWindows.getInstance().m_main.m_main_chengjiu.setState(false);
				
//				refreshRight();
				UIWindows.getInstance().m_main.m_imageBox_lingjiang.setShow(false);
				UIWindows.getInstance().m_main.m_lable_lingjiang.setShow(false);
				
				UIWindows.getInstance().m_main.m_imageBox_chengjiu.setShow(true);
				UIWindows.getInstance().m_main.m_imageBox_chengjiu.setSelect(true);
				UIWindows.getInstance().m_main.m_imageBox_chengjiu.setSelectType(DWImageBox.SELECT_TYPE_ARC3);
				UIWindows.getInstance().m_main.m_imageBox_chengjiu.setTouch(true);
				return;
			}
		}

	}
	public void sendAchieve(){
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		DataOutputStream dos = new DataOutputStream(baos);
		try {
			dos.writeByte(0);
			DWGameManager
					.getInstance()
					.getConnection()
					.sendMessage(0, GameProtocol.CS_ACHIEVEMENT_ALL,
							baos.toByteArray());
			Tools.debugPrintln("发送指令" + GameProtocol.CS_ACHIEVEMENT_ALL + "!");
			dos.close();
			dos = null;
			baos.close();
			baos = null;
		}
		catch (IOException e) {
			Tools.debug(e);
		}
	}
	public void sendLingqu(short id){
		m_lingquid = id;
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		DataOutputStream dos = new DataOutputStream(baos);
		try {
			dos.writeShort(id);
			DWGameManager
					.getInstance()
					.getConnection()
					.sendMessage(0, GameProtocol.CS_ACHIEVEMENT_GET,
							baos.toByteArray());
			Tools.debugPrintln("发送指令" + GameProtocol.CS_ACHIEVEMENT_GET + "!");
			dos.close();
			dos = null;
			baos.close();
			baos = null;
		}
		catch (IOException e) {
			Tools.debug(e);
		}
	}
	private ArrayList<Achievement> m_arrayList[];
	
	
	
	private void initdrawchengjiu(){
		
	}
	
	
	public void initData() throws IOException {
		
		m_button_str = new String[]{"1-10级","11-20级","21-30级","31-40级","41-50级","51-60级","61-80级"};
		m_button_size = m_button_str.length;//按钮个数
		

		 String file = "/date/achievement.bin";
		 
//		AssetManager manger= DWGameManager.getInstance().getCurrentActivity().getAssets();
//		String[] list = manger.list("res");
//		for(int i=0;i<list.length;i++){
//			Tools.debugPrintln("目录下的所有:"+list[i]);
//		}
	       
//		AssetFileDescriptor df = DWGameManager.getInstance().getCurrentActivity().getAssets().openFd(file);
		
			
		DataInputStream dis = new DataInputStream(new FileInputStream(DWGameManager.s_res_dir+file));
		int size = dis.readInt();
		m_ach = new Achievement[size];
		for (int i = 0; i < size; i++) {
			
			
			m_ach[i] = new Achievement();
			m_ach[i].setId(dis.readInt());// 成就ID
			m_ach[i].setTitle(dis.readUTF());// 成就标题
			m_ach[i].setCondition(dis.readUTF());// 成就要求描述，限制10个字
			m_ach[i].setPrize(dis.readUTF());// 成就要求描述，限制10个字
			m_ach[i].setPlate(dis.readInt());// 奖杯个数
			m_ach[i].setGroup(dis.readInt());// 所在的组
			m_ach[i].setOrder(dis.readInt());// 所在组的序列

		}
	
		getArrayListGroup();
		
	}
	

}
